package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName: OrderStatisticsTask
 * @Package: com.atguigu.spzx.manager.task
 * @Description 定时任务类
 * @Author 孤妤
 * @Date: 2023/12/10 21:58
 */
@Component
@Slf4j
public class OrderStatisticsTask {
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;
    /**
     * @description: 每天凌晨两点查询当天的订单总额和数量
     **/
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        // 获取昨天的日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");
        // 通过该日期去查询订单总额与数量
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);
        // 查询到了添加到订单汇总表中
        if(orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics) ;
        }
    }
}
